﻿using System;
using System.Drawing;
using System.IO;
using System.Text;
using EastFantasy.DnfAccountScan.Public;

namespace EastFantasy.DnfAccountScan.Main
{
    public partial class ImageCode : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        /// <summary>
        /// 定义传值验证码委托
        /// </summary>
        /// <param name="imageCode">验证码字符串</param>
        public delegate void TransImageCode(string imageCode);
        public event TransImageCode transImageCode;//定义传值验证码的事件

        /// <summary>
        /// 定义不显示验证码窗体委托
        /// </summary>
        /// <param name="DisableImage">是否不显示验证码</param>
        /// <param name="Cancel">取消按钮</param>
        public delegate void ShiledImageCode(bool DisableImage, bool Cancel);
        public event ShiledImageCode shiledImageCode;//定义不显示验证码的事件

        public ImageCode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="account"></param>
        public void GetWebImage(string account, HttpHelper helper, Random random)
        {
            string url = string.Format("http://captcha.qq.com/getimage?aid=1003903&r={0}&uin={1}", random.NextDouble(), account);
            Stream stream = helper.GetStream(url, HttpHelper.CookieContainer);
            picImageCode.Image = Image.FromStream(stream);
        }

        #region 窗体激活
        /// <summary>
        /// 窗体激活
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageCode_Activated(object sender, EventArgs e)
        {
            txtImageCode.Focus();
            txtImageCode.SelectAll();
        }
        #endregion

        #region 代码转换成验证码

        /// 代码转换成验证码
        /// <summary>
        /// 代码转换成验证码
        /// </summary>
        /// <param name="pageData"></param>
        public void CodeToImage(Byte[] pageData)
        {
            Byte[] sch = Encoding.Default.GetBytes("&VC=");
            for (int i = 0; i < pageData.Length - 10; i++)
            {
                if (pageData[i] == sch[0] && pageData[i + 1] == sch[1] && pageData[i + 2] == sch[2] && pageData[i + 3] == sch[3])
                {
                    int len = pageData.Length - (i + 4);
                    Byte[] arr = new Byte[len];
                    Array.Copy(pageData, i + 4, arr, 0, len);
                    string s1 = System.Text.Encoding.ASCII.GetString(arr);
                    byte[] imArr = StringToByteArray(s1);
                    MemoryStream stream = new MemoryStream(imArr);
                    //显示验证码
                    picImageCode.Image = Image.FromStream(stream);
                }
            }
        }
        #endregion

        #region StringToByteArray
        /// <summary>
        /// StringToByteArray
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] StringToByteArray(string data)
        {
            byte[] bData = null;
            string sData = data;
            if (sData.Length > 0)
            {
                bData = new byte[sData.Length / 2];
                for (int nJ = 0; nJ < sData.Length; nJ += 2)
                    bData[nJ / 2] = byte.Parse(sData.Substring(nJ, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }

            return bData;
        }
        #endregion

        #region  输入验证码
        /// <summary>
        /// 输入验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtImageCode_TextChanged(object sender, EventArgs e)
        {
            if (txtImageCode.Text.Length == 4)
            {
                if (transImageCode != null)
                    transImageCode(txtImageCode.Text.Trim());
                Close();
            }
        }
        #endregion

        #region 关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (shiledImageCode != null)
                shiledImageCode(!Convert.ToBoolean(cbDisableImage.EditValue), false);
            Close();
        }
        #endregion

    }
}